"""
-*- coding: utf-8 -*-
@Time : 2021/11/28 19:32
@Author : XGT-Python
"""

import requests
import csv  # 保存数据

file = open('data2.csv', mode='a', encoding='utf-8', newline='')
csv_write = csv.DictWriter(file,
                           fieldnames=['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率', '市盈率(TTM)',
                                       '股息率', '市值'])
csv_write.writeheader()

# 确定url地址
for page in range(1, 56):
    url = f'https://xueqiu.com/service/v5/stock/screener/quote/list?page={page}&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1638099481187'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    json_data = response.json()
    # print(json_data)
    # 数据解析
    data_list = json_data['data']['list']
    for data in data_list:
        data1 = data['symbol']
        data2 = data['name']
        data3 = data['current']
        data4 = data['chg']
        data5 = data['percent']
        data6 = data['current_year_percent']
        data7 = data['volume']
        data8 = data['amount']
        data9 = data['turnover_rate']
        data10 = data['pe_ttm']
        data11 = data['dividend_yield']
        data12 = data['market_capital']
        print(data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12)
        ['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率', '市盈率(TTM)', '股息率', '市值']
        data_dict = {
            '股票代码': data1,
            '股票名称': data2,
            '当前价': data3,
            '涨跌额': data4,
            '涨跌幅': data5,
            '年初至今': data6,
            '成交量': data7,
            '成交额': data8,
            '换手率': data9,
            '市盈率(TTM)': data10,
            '股息率': data11,
            '市值': data12,
        }
        csv_write.writerow(data_dict)
file.close()
